/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.web.controller;

import com.intelligent.ispc.common.dto.JsonPageResponseDto;
import com.intelligent.ispc.common.dto.JsonResponseDto;
import com.intelligent.ispc.common.utils.ExceptionMessages;
import com.intelligent.ispc.common.web.Servlets;
import com.intelligent.ispc.core.dto.DUTAttributeDto;
import com.intelligent.ispc.core.dto.TestDataExDto;
import com.intelligent.ispc.core.dto.TestItemDto;
import com.intelligent.ispc.core.dto.YieldDto;
import com.intelligent.ispc.core.service.ReportService;
import com.intelligent.ispc.utils.SystemConfig;
import com.intelligent.ispc.web.utils.PageUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by cherry on 2016/2/2.
 */
@Controller
@RequestMapping(value = "/yield")
public class YieldReportController {

    @Autowired
    private ReportService reportService;

    private Integer draw = 0;

    @Autowired
    private SystemConfig systemConfig;

    @RequestMapping(value = "/gotoYield", method = RequestMethod.GET)
    public String yieldReport(Model model) {
        return "analysis/queryYield";
    }

    @ResponseBody
    @RequestMapping(value = "/queryAttributeYield", method = RequestMethod.POST)
    public JsonPageResponseDto attrQuery(Model model, ServletRequest request) {
        try {
            Subject subject = SecurityUtils.getSubject();
            if (!(subject.isPermitted("analysis:report:view"))) {
                return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
            }
            Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");
            List<DUTAttributeDto> dutAttributeDtoList = reportService.searchAttributes(searchParam);
            JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(dutAttributeDtoList);
            json.setRecordsTotal(dutAttributeDtoList.size());
            json.setRecordsFiltered(dutAttributeDtoList.size());
            json.setDraw(draw);
            return json;
        } catch (Exception e) {
            return JsonPageResponseDto.FAILURE().setMessage(e.getMessage());
        }
    }

    @ResponseBody
    @RequestMapping(value = "/queryItemYield", method = RequestMethod.POST)
    public JsonPageResponseDto itemQuery(Model model, ServletRequest request) {
        try {
            Subject subject = SecurityUtils.getSubject();
            if (!(subject.isPermitted("analysis:report:view"))) {
                return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
            }
            Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");
            Page<TestItemDto> page = reportService.searchTestItem(searchParam, PageUtil.buildPageWithParameters(request, systemConfig.getPageSize()));
            List<TestItemDto> list = page.getContent();

            JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(list);
            json.setRecordsTotal(page.getTotalElements());
            json.setRecordsFiltered(page.getTotalElements());
            json.setDraw(0);
            return json;
        } catch (Exception e) {
            return JsonPageResponseDto.FAILURE().setMessage(e.getMessage());
        }

    }

    @ResponseBody
    @RequestMapping(value = "/queryYields", method = RequestMethod.POST)
    public JsonPageResponseDto yieldQuery(Model model, ServletRequest request) {
        try {
            Subject subject = SecurityUtils.getSubject();
            if (!subject.isPermitted("analysis:report:view")) {
                return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
            }

            Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");
            List<YieldDto> yieldDtoList = reportService.searchYield(searchParam);
            JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(yieldDtoList);
            json.setDraw(draw);
            return json;
        } catch (Exception e) {
            return JsonPageResponseDto.FAILURE().setMessage(e.getMessage());
        }
    }

    @RequestMapping(value = "/gotoHistoryYield", method = RequestMethod.GET)
    public String jumptoHistory(Model model, ServletRequest request) {

        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("analysis:report:view")) {
            return "error/noPermission";
        }
        return "analysis/historyYieldShow";
    }

    @ResponseBody
    @RequestMapping(value = "/queryHistoryYield", method = RequestMethod.POST)
    public JsonPageResponseDto queryYieldHistory(Model model, ServletRequest request) {
        try {
            Subject subject = SecurityUtils.getSubject();
            if (!subject.isPermitted("analysis:report:view")) {
                return JsonPageResponseDto.FAILURE().setMessage(ExceptionMessages.EXCEPTION_NO_PERMISSION);
            }

            Map<String, Object> searchParam = Servlets.getParametersStartingWith(request, "search_");
            //JdbcPageable<TestDataExDto> pageable = reportService.searchYieldSerialNumber(searchParam, size, pageNum);
            Pageable page =  PageUtil.buildPageWithParameters(request, systemConfig.getPageSize());
            searchParam.put("page", page.toString());
            Page<TestDataExDto> pageable = reportService.searchYieldSerialNumber(searchParam, page);
            JsonPageResponseDto json = JsonPageResponseDto.SUCCESS().setBody(pageable.getContent());
            json.setRecordsTotal(pageable.getTotalElements());
            json.setRecordsFiltered(pageable.getTotalElements());
            json.setDraw(draw);
            return json;
        } catch (Exception e) {
            return JsonPageResponseDto.FAILURE().setMessage(e.getMessage());
        }
    }

    @RequestMapping(value = "/gotoItemsAttrYield", method = RequestMethod.GET)
    public String jumpToYieldStation(Model model, ServletRequest request) {

        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted("analysis:report:view")) {
            return "error/noPermission";
        }
        return "analysis/itemsAndAttributeYield";
    }

}
